package lasec.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import lasec.connect.DBManager;

/**
 * Servlet implementation class RateServlet
 */
@WebServlet("/RateServlet")
public class RateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private int serviceCounter = 0;
    private boolean shuttingDown;
	private DBManager manage;  
	
	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		manage = new DBManager();
		System.out.println("Init()");
	}

	protected synchronized void enteringServiceMethod() {
		serviceCounter++;
	}
	
	protected synchronized void leavingServiceMethod() {
		serviceCounter--;
	}
	
	protected synchronized int numServices() {
		return serviceCounter;
	}
	
	protected synchronized void setShuttingDown(boolean flag) {
		shuttingDown = flag;
	}
	
	protected synchronized boolean isShuttingDown() {
		return shuttingDown;
	}
	
	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
		if (numServices() > 0) {
			setShuttingDown(true);
		}
		
		while (numServices() > 0) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
		}
		System.out.println("Destroying..." + this.getClass());
		manage = null;
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		enteringServiceMethod();
		try {
			super.service(request, response);
		} finally {
			leavingServiceMethod();
		}
	}
	
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uid = request.getParameter("uid");
		String mid = request.getParameter("mid");
		String value = request.getParameter("value");
		
		try {
			manage.insertMovieRate(uid, mid, value);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("User " + uid + " said  movie " + mid + " is worth " + value);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

}
